package cn.stylefeng.guns.modular.business.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.business.entity.RiverAdministration;
import cn.stylefeng.guns.modular.business.enums.RiverAdministrationExceptionEnum;
import cn.stylefeng.guns.modular.business.mapper.RiverAdministrationMapper;
import cn.stylefeng.guns.modular.business.pojo.RiverAdministrationRequest;
import cn.stylefeng.guns.modular.business.service.RiverAdministrationService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 河政业务实现层
 *
 * @author zhangyaowen
 * @date 2021/10/08 17:17
 */
@Service
public class RiverAdministrationServiceImpl extends ServiceImpl<RiverAdministrationMapper, RiverAdministration> implements RiverAdministrationService {

	@Override
    public void add(RiverAdministrationRequest riverAdministrationRequest) {
        RiverAdministration riverAdministration = new RiverAdministration();
        BeanUtil.copyProperties(riverAdministrationRequest, riverAdministration);
        this.save(riverAdministration);
    }

    @Override
    public void del(RiverAdministrationRequest riverAdministrationRequest) {
        RiverAdministration riverAdministration = this.queryRiverAdministration(riverAdministrationRequest);
        this.removeById(riverAdministration.getId());
    }

    @Override
    public void edit(RiverAdministrationRequest riverAdministrationRequest) {
        RiverAdministration riverAdministration = this.queryRiverAdministration(riverAdministrationRequest);
        BeanUtil.copyProperties(riverAdministrationRequest, riverAdministration);
        this.updateById(riverAdministration);
    }

    @Override
    public RiverAdministration detail(RiverAdministrationRequest riverAdministrationRequest) {
        return this.queryRiverAdministration(riverAdministrationRequest);
    }

    @Override
    public PageResult<RiverAdministration> findPage(RiverAdministrationRequest riverAdministrationRequest) {
        LambdaQueryWrapper<RiverAdministration> wrapper = createWrapper(riverAdministrationRequest);
        Page<RiverAdministration> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<RiverAdministration> findList(RiverAdministrationRequest riverAdministrationRequest) {
        LambdaQueryWrapper<RiverAdministration> wrapper = this.createWrapper(riverAdministrationRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author zhangyaowen
     * @date 2021/10/08 17:17
     */
    private RiverAdministration queryRiverAdministration(RiverAdministrationRequest riverAdministrationRequest) {
        RiverAdministration riverAdministration = this.getById(riverAdministrationRequest.getId());
        if (ObjectUtil.isEmpty(riverAdministration)) {
            throw new ServiceException(RiverAdministrationExceptionEnum.RIVERADMINISTRATION_NOT_EXISTED);
        }
        return riverAdministration;
    }

    /**
     * 创建查询wrapper
     *
     * @author zhangyaowen
     * @date 2021/10/08 17:17
     */
    private LambdaQueryWrapper<RiverAdministration> createWrapper(RiverAdministrationRequest riverAdministrationRequest) {
        LambdaQueryWrapper<RiverAdministration> queryWrapper = new LambdaQueryWrapper<>();

        Long id = riverAdministrationRequest.getId();
        String className = riverAdministrationRequest.getClassName();
        String raFirst = riverAdministrationRequest.getRaFirst();
        String raSecond = riverAdministrationRequest.getRaSecond();
        String raThrid = riverAdministrationRequest.getRaThrid();
        String historyDating = riverAdministrationRequest.getHistoryDating();
        String theCommon = riverAdministrationRequest.getTheCommon();
        String raManagement = riverAdministrationRequest.getRaManagement();
        String quoteAnnotation = riverAdministrationRequest.getQuoteAnnotation();
        String raAnnalsfrom = riverAdministrationRequest.getRaAnnalsfrom();
        String raAnnalspage = riverAdministrationRequest.getRaAnnalspage();

        queryWrapper.eq(ObjectUtil.isNotNull(id), RiverAdministration::getId, id);
        queryWrapper.like(ObjectUtil.isNotEmpty(className), RiverAdministration::getClassName, className);
        queryWrapper.like(ObjectUtil.isNotEmpty(raFirst), RiverAdministration::getRaFirst, raFirst);
        queryWrapper.like(ObjectUtil.isNotEmpty(raSecond), RiverAdministration::getRaSecond, raSecond);
        queryWrapper.like(ObjectUtil.isNotEmpty(raThrid), RiverAdministration::getRaThrid, raThrid);
        queryWrapper.like(ObjectUtil.isNotEmpty(historyDating), RiverAdministration::getHistoryDating, historyDating);
        queryWrapper.like(ObjectUtil.isNotEmpty(theCommon), RiverAdministration::getTheCommon, theCommon);
        queryWrapper.like(ObjectUtil.isNotEmpty(raManagement), RiverAdministration::getRaManagement, raManagement);
        queryWrapper.like(ObjectUtil.isNotEmpty(quoteAnnotation), RiverAdministration::getQuoteAnnotation, quoteAnnotation);
        queryWrapper.like(ObjectUtil.isNotEmpty(raAnnalsfrom), RiverAdministration::getRaAnnalsfrom, raAnnalsfrom);
        queryWrapper.like(ObjectUtil.isNotEmpty(raAnnalspage), RiverAdministration::getRaAnnalspage, raAnnalspage);

        return queryWrapper;
    }

}